package com.wondertek.onvif.entity;

import javax.persistence.*;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import com.fasterxml.jackson.annotation.JsonIgnore;

import java.time.LocalDateTime;
import java.util.List;

/**
 * ONVIF设备实体类
 * 
 * @author wondertek
 * @version 1.0.0
 */
@Data
@Entity
@Table(name = "onvif_device")
@EqualsAndHashCode(callSuper = false)
public class OnvifDevice {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 设备名称
     */
    @Column(nullable = false, length = 100)
    private String name;

    /**
     * 设备ID - ONVIF设备的唯一标识
     */
    @Column(nullable = false, unique = true, length = 100)
    private String deviceId;

    /**
     * 设备IP地址
     */
    @Column(nullable = false, length = 45)
    private String ipAddress;

    /**
     * ONVIF端口
     */
    @Column(nullable = false)
    private Integer port = 80;

    /**
     * 用户名
     */
    @Column(length = 50)
    private String username;

    /**
     * 密码
     */
    @Column(length = 100)
    private String password;

    /**
     * 设备服务地址
     */
    @Column(length = 500)
    private String serviceUrl;

    /**
     * 设备制造商
     */
    @Column(length = 100)
    private String manufacturer;

    /**
     * 设备型号
     */
    @Column(length = 100)
    private String model;

    /**
     * 固件版本
     */
    @Column(length = 50)
    private String firmwareVersion;

    /**
     * 序列号
     */
    @Column(length = 100)
    private String serialNumber;

    /**
     * 硬件ID
     */
    @Column(length = 100)
    private String hardwareId;

    /**
     * 设备状态：ONLINE, OFFLINE, ERROR
     */
    @Enumerated(EnumType.STRING)
    @Column(nullable = false)
    private DeviceStatus status = DeviceStatus.OFFLINE;

    /**
     * 是否启用
     */
    @Column(nullable = false)
    private Boolean enabled = true;

    /**
     * 设备描述
     */
    @Column(length = 500)
    private String description;

    /**
     * 设备类型（Camera, NVR, DVR等）
     */
    @Column(length = 50)
    private String deviceType;

    /**
     * 设备位置
     */
    @Column(length = 200)
    private String location;

    /**
     * MAC地址
     */
    @Column(length = 17)
    private String macAddress;

    /**
     * 最后在线时间
     */
    private LocalDateTime lastOnlineTime;

    /**
     * 创建时间
     */
    @CreationTimestamp
    @Column(nullable = false, updatable = false)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @UpdateTimestamp
    @Column(nullable = false)
    private LocalDateTime updateTime;

    /**
     * 设备通道列表
     */
    @OneToMany(mappedBy = "device", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JsonIgnore
    private List<OnvifChannel> channels;

    /**
     * 手动添加getter和setter方法以解决Lombok编译问题
     */
    public Long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDeviceId() {
        return deviceId;
    }

    public void setDeviceId(String deviceId) {
        this.deviceId = deviceId;
    }

    public String getIpAddress() {
        return ipAddress;
    }

    public void setIpAddress(String ipAddress) {
        this.ipAddress = ipAddress;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getServiceUrl() {
        return serviceUrl;
    }

    public void setServiceUrl(String serviceUrl) {
        this.serviceUrl = serviceUrl;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public DeviceStatus getStatus() {
        return status;
    }

    public void setStatus(DeviceStatus status) {
        this.status = status;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

    public List<OnvifChannel> getChannels() {
        return channels;
    }

    public void setChannels(List<OnvifChannel> channels) {
        this.channels = channels;
    }

    public String getFirmwareVersion() {
        return firmwareVersion;
    }

    public void setFirmwareVersion(String firmwareVersion) {
        this.firmwareVersion = firmwareVersion;
    }

    public String getSerialNumber() {
        return serialNumber;
    }

    public void setSerialNumber(String serialNumber) {
        this.serialNumber = serialNumber;
    }

    public String getHardwareId() {
        return hardwareId;
    }

    public void setHardwareId(String hardwareId) {
        this.hardwareId = hardwareId;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDeviceType() {
        return deviceType;
    }

    public void setDeviceType(String deviceType) {
        this.deviceType = deviceType;
    }

    public String getLocation() {
        return location;
    }

    public void setLocation(String location) {
        this.location = location;
    }

    public String getMacAddress() {
        return macAddress;
    }

    public void setMacAddress(String macAddress) {
        this.macAddress = macAddress;
    }

    public LocalDateTime getLastOnlineTime() {
        return lastOnlineTime;
    }

    public void setLastOnlineTime(LocalDateTime lastOnlineTime) {
        this.lastOnlineTime = lastOnlineTime;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public LocalDateTime getUpdateTime() {
        return updateTime;
    }

    /**
     * 设备状态枚举
     */
    public enum DeviceStatus {
        ONLINE("在线"),
        OFFLINE("离线"),
        ERROR("错误");

        private final String description;

        DeviceStatus(String description) {
            this.description = description;
        }

        public String getDescription() {
            return description;
        }
    }
}